from datetime import datetime
from uuid import UUID, uuid4
from ourblockchain.blockchain import Base
from ourblockchain.content import Content


class Transaction(Base):
    def __init__(self,
                 sender: str, receiver: str,
                 amount: int, t: float = None,
                 prev_hash: str = None, transaction_id: str = None,
                 content: dict = None):
        assert UUID(sender, version=4)
        assert UUID(receiver, version=4)
        assert transaction_id is None or UUID(transaction_id, version=4)

        content = Content(**content)
        assert content is None or content.purchase_type in ['own', 'transship', 'setPrice']

        self.content = content

        self.id = transaction_id if transaction_id is not None else str(uuid4())
        self.sender = sender
        self.receiver = receiver
        self.amount = amount
        self.t = t if t is not None else datetime.now().timestamp()
        self.prev_hash = prev_hash

    def __eq__(self, other):
        return \
            self.id == other.id and \
            self.sender == other.sender and \
            self.receiver == other.receiver and \
            self.amount == other.amount and \
            self.content == other.content

    def __hash__(self):
        return hash((self.id, self.sender, self.receiver, self.amount, self.content))
